<template>
	<view>
		<button type="primary" @tap="login" open-type="getUserProfile">登录系统</button>
		<button @tap="test">测试</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			login(){
				let that = this
				//login方法获取code，code5分钟有效
				uni.login({
					provider:'weixin',
					success:function(res){
						let code = res.code;
						that.code = code;
						console.log("code=>>>"+code)
					}
				}),
				//获取微信用户信息
				uni.getUserProfile({
					desc:"获取登录用户的信息",
					success(res){
						console.log(res)
						let username = res.userInfo.nickName;
						let photo = res.userInfo.avatarUrl;
						console.log("username====>"+username);
						console.log("photo====>"+photo);
						that.username = username;
						that.photo =photo;
						//请求后台商户系统，返回用户token，存入storage，以后每次请求商户系统都要带有token
						uni.request({
							url:that.url.wxLogin,
							method:"POST",
							data:{
								"nickname":that.username,
								"photo":that.photo,
								"code":that.code,
							},
							success(res){
								let token = res.data.token;
								let expire = res.data.expire;
								console.log("token====>"+token)
								console.log("expire====>"+expire)
								uni.setStorageSync("token",token);
								uni.setStorageSync("expire",expire);
								uni.switchTab({
									url:"../index/index",
								})
							}
						})
					}
				})
				
			}
		},
	}
</script>

<style>

</style>
